/**
 * Copyright (C) 2015-2016 Guangzhou Xunhong Network Technology Co., Ltd. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jxccp.ui.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jxccp.ui.R;
import com.jxccp.ui.entities.JXOrderInfo;
import com.jxccp.ui.entities.ZhuiyiProductEntity;
import com.jxccp.ui.entities.ZhuiyiProductOrderEntity;

import java.util.List;

public class ZhuiyiProductOrderListAdapter extends JXBasicAdapter<ZhuiyiProductOrderEntity,
        ListView> {

    private LayoutInflater mInflater;

    String[] productOrderStatus;

    public ZhuiyiProductOrderListAdapter(Context context, List<ZhuiyiProductOrderEntity> list) {
        super(context, list);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        productOrderStatus = context.getResources().getStringArray(R.array
                .jx_zhuiyi_product_order_status);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jx_zhuiyi_product_order_item, null);
        }
        ZhuiyiProductOrderEntity entity = list.get(position);
        TextView shopName = (TextView) convertView.findViewById(R.id.tv_shopName);
        TextView orderStatus = (TextView) convertView.findViewById(R.id.tv_order_status);
        TextView orderNumSum = (TextView) convertView.findViewById(R.id.tv_product_num_sum);
        TextView orderTotalPay = (TextView) convertView.findViewById(R.id.tv_product_price_sum);
        shopName.setText(entity.getShopName());
        orderNumSum.setText("共" + entity.getProductList().size() + "件商品");
        orderTotalPay.setText("实付：￥" + entity.getOrderTotalPay());
        if (!TextUtils.isEmpty(entity.getOrderStatus())){
            orderStatus.setVisibility(View.VISIBLE);
            switch (entity.getOrderStatus()) {
                case ZhuiyiProductOrderEntity.ORDER_STATUS_WAIT_SELLER_SEND_GOODS:
                    orderStatus.setText(productOrderStatus[0]);
                    break;
                case ZhuiyiProductOrderEntity.ORDER_STATUS_WAIT_BUYER_PAY:
                    orderStatus.setText(productOrderStatus[1]);
                    break;
                case ZhuiyiProductOrderEntity.ORDER_STATUS_WAIT_BUYER_CONFIRM_GOODS:
                    orderStatus.setText(productOrderStatus[2]);
                    break;
                case ZhuiyiProductOrderEntity.ORDER_STATUS_TRADE_CLOSED:
                    orderStatus.setText(productOrderStatus[3]);
                    break;
                case ZhuiyiProductOrderEntity.ORDER_STATUS_TRADE_BUYER_SIGNED:
                    orderStatus.setText(productOrderStatus[4]);
                    break;
                default:
                    orderStatus.setText(productOrderStatus[3]);
                    break;
            }
        }else {
            orderStatus.setVisibility(View.INVISIBLE);
        }
        LinearLayout productContent = convertView.findViewById(R.id.ll_product_content);
        productContent.removeAllViews();
        if (entity.getProductList() != null && entity.getProductList().size() > 0) {
            for (ZhuiyiProductEntity productEntity : entity.getProductList()) {
                View view = mInflater.inflate(R.layout.jx_zhuiyi_product_item, null);
                TextView productName = (TextView) view.findViewById(R.id.tv_product_name);
                TextView productDes = (TextView) view.findViewById(R.id.tv_product_des);
                TextView productPrice = (TextView) view.findViewById(R.id.tv_product_price);
                TextView productNum = (TextView) view.findViewById(R.id.tv_product_num);
                ImageView productImg = (ImageView) view.findViewById(R.id.iv_product_img);
                productName.setText(productEntity.getProductName());
                productDes.setText(productEntity.getProductdescription());
                productPrice.setText("￥" + productEntity.getProductPrice());
                productNum.setText("x" + productEntity.getProductNum());
                Glide.with(context)
                        .load(productEntity.getProductImgUrl())
                        .apply(new RequestOptions().placeholder(R.drawable.jx_ic_photo_default)
                                .fitCenter())
                        .into(productImg);
                productContent.addView(view);
            }
        }
        return convertView;
    }

    public void refresh(List<ZhuiyiProductOrderEntity> orderList) {
        this.list.clear();
        this.list.addAll(orderList);
        notifyDataSetChanged();
    }

}
